<template>
    <!-- 对话框表单 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form ref="formRef" @keyup.enter="baTable.onSubmit(formRef)" :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'" :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules" v-if="!baTable.form.loading">
                    <FormItem
                        label="VIP分类"
                        v-model="baTable.form.items!.vip_type_id"
                        prop="vip_type_id"
                        type="remoteSelect"
                        :key="('group-' + baTable.form.items!.id)"
                        :input-attr="{
                            params: { isTree: false },
                            field: 'title',
                            'remote-url': '/admin/v1/page/vip/list',
                            placeholder: t('Click select'),
                        }"
                    />
                    <FormItem
                        label="VIP天数"
                        v-model="baTable.form.items!.vip_day_id"
                        prop="vip_day_id"
                        type="remoteSelect"
                        :key="('group-' + baTable.form.items!.id)"
                        :input-attr="{
                            params: { isTree: false },
                            field: 'title',
                            'remote-url': '/admin/v1/page/vip_day/list',
                            placeholder: t('Click select'),
                        }"
                    />
                    <el-form-item prop="title" label="名称">
                        <el-input v-model="baTable.form.items!.title" type="string" placeholder="请输入名称如1年素材VIP"></el-input>
                    </el-form-item>
                    <el-form-item prop="price" label="价格">
                        <el-input v-model="baTable.form.items!.price" type="string" placeholder="请输入价格"></el-input>
                    </el-form-item>
                    <FormItem
                        label="显示"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :data="{ content: { '0': '隐藏', '1': '显示' }, childrenAttr: { border: true } }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                        t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import { buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    title: [buildValidatorData({ name: 'required', title: '名称' })],
    icon: [buildValidatorData({ name: 'required', title: '图标' })],
})
</script>

<style scoped lang="scss"></style>
